package context.dao;

import context.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@Repository
public class UserRepository {
    private final String SELECT_BY_ID = "select * from user where id=?";
    private final String SELECT_BY_USERNAME = "select * from user where username=?";
    private final String INSERT_USER = "insert into user(username,password) values(?,?)";

    private JdbcTemplate jdbcTemplate;

    @Autowired          //自动装配
    public UserRepository(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public User findUserByID(long id){

        try{
            return jdbcTemplate.queryForObject(SELECT_BY_ID,this::mapRow ,id);
        }catch (EmptyResultDataAccessException e){
            System.out.println("select user null");
            return null;
        }

    }

    public User findUserByUsername(String username){
        User user=null;
        try {
            user = jdbcTemplate.queryForObject(SELECT_BY_USERNAME,this::mapRow, username);
            return user;
        }catch (EmptyResultDataAccessException e){
            return null;
        }
    }

    private User mapRow(ResultSet resultSet, int i) throws SQLException {
        User result = new User();
        result.setId(resultSet.getLong("id"));
        result.setUsername(resultSet.getString("username"));
        result.setPassword(resultSet.getString("password"));
        return result;
    }

    public long insertUser(final String password, final String username) {

        KeyHolder keyHolder = new GeneratedKeyHolder();
        jdbcTemplate.update(
                new PreparedStatementCreator() {
                    public PreparedStatement createPreparedStatement(Connection con) throws SQLException
                    {
                        PreparedStatement ps = con.prepareStatement(INSERT_USER, new String[]{"username", "password"});
                        ps.setString(1, username);
                        ps.setString(2,password);
                        return ps;
                    }
                }, keyHolder);
        System.out.println("自动插入id============================" + keyHolder.getKey().intValue());
        return keyHolder.getKey().longValue();
    }

    //改密码
    private static final String sql = "update user set password=? where userid = ? ";

    public void updateUserPassword(int userid, String password) {
        Object args[] = new Object[]{userid, password};
        int temp = jdbcTemplate.update(sql, args);
        if (temp > 0) {
            System.out.println("更新成功");
        } else {
            System.out.println("更新失败");
        }

    }

    private String selectName = "select username from user where id=?";

    public String getUsername(long userid) {
        return jdbcTemplate.queryForObject(selectName, new RowMapper<String>() {
            @Override
            public String mapRow(ResultSet resultSet, int i) throws SQLException {
                return resultSet.getString(1);
            }
        }, userid);
    }


    private final String GET_USER_INFO = "SELECT * FROM contest.user join userinformation on user.id=userinformation.userid where user.id=?";

    public User getAllUserInfo(long userid) {
        try {
            return jdbcTemplate.queryForObject(GET_USER_INFO, new RowMapper<User>() {
                @Override
                public User mapRow(ResultSet resultSet, int rowNum) throws SQLException {
                    User result = new User();
                    result.setId(resultSet.getLong("id"));
                    result.setUsername(resultSet.getString("username"));
                    result.setPassword(resultSet.getString("password"));
                    result.setPhoneNumber(resultSet.getString("phoneNumber"));
                    result.setGender(resultSet.getString("gender"));
                    result.setAge(resultSet.getInt("age"));
                    result.setWork(resultSet.getString("work"));
                    result.setQq(resultSet.getString("qq"));
                    result.setDataVisibility(resultSet.getInt("dataVisibility"));
                    result.setUserimg(resultSet.getString("img"));
                    return result;
                }
            }, userid);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }
}
